package com.fishsb19.asuscomm.filereadandwrite;

import java.io.*;

import com.fishsb19.asuscomm.hashtable.MyHashTableInterface;

public class MyFileReader {
	/**
	 * Parameters
	 */
	private BufferedReader strContentReader;
	private String tempStr;
	private Reader reader;
	private MyHashTableInterface myHTinterface;
	private String searchTarget;
	private String deleteTarget;

	/**
	 * Constructor, used to initialize parameters,load in the file
	 * 
	 * @param importFile
	 * @throws IOException
	 */
	public MyFileReader(Reader importFile) throws IOException {
		this.tempStr = null;
		this.strContentReader = null;
		this.reader = importFile;

	}

	public void fileReader() throws IOException {
		// Initialize the bufferReader ,load the file reader's instance
		this.strContentReader = new BufferedReader(this.reader);
		int tempCounter = 0;
		int closestPrime = 0;
		int findPrimeCounter = 0;
		// Mark the beginning of file with big enough buffer in case of overflow
		this.strContentReader.mark(999999);
		/**
		 * Compute the number of non-instruction line
		 */
		while ((tempStr = this.strContentReader.readLine()) != null) {

			if (tempStr.contains("SEARCH") || tempStr.contains("DELETE")) {
				if (tempStr.contains("SEARCH")) {
					System.out.println("Instruction SEARCH Found");
				} else if (tempStr.contains("DELETE")) {
					System.out.println("Instruction DELETE found");
				}
				continue;
			} else {
				tempCounter = tempCounter + 1;
				System.out.println("This is current non-instruction string "
						+ tempStr + " " + tempCounter);

			}
		}
		System.out.println("this is current non-compute tempCounter "
				+ tempCounter);
		// Back to the first line
		this.strContentReader.reset();
		outer: for (closestPrime = tempCounter;; closestPrime++) {
			for (findPrimeCounter = 2; findPrimeCounter < closestPrime; findPrimeCounter++) {
				if (closestPrime % findPrimeCounter == 0)
					break;
			}
			if (findPrimeCounter == closestPrime)
				break outer;
		}
		System.out.println("This is the closestPrime  " + closestPrime);
		this.myHTinterface = new MyHashTableInterface(closestPrime);
		while ((tempStr = this.strContentReader.readLine()) != null) {
			// delete the head and tail whitespace which were read by readline
			// function
			tempStr = tempStr.trim();
			if (tempStr.contains("SEARCH")) {
				System.out.println("Found SEARCH");
				searchTarget = tempStr.substring(7);
				System.out.println("This is target string after manipulated "
						+ searchTarget);
				this.myHTinterface.searchData(searchTarget);
			} else if (tempStr.contains("DELETE")) {
				System.out.println("Found DELETE");
				deleteTarget = tempStr.substring(7);
				System.out.println("This is target string after manipulated "
						+ deleteTarget);
				this.myHTinterface.deleteData(deleteTarget);
			} else if (tempStr.equals("####")) {
				System.out
						.println("Invaild string ####, it will be ignored by the program ");
			} else {
				this.myHTinterface.insertData(tempStr);
			}
		}
		this.myHTinterface.getAverageProbeSearch();
		//this.myHTinterface.checkHashTable();
		this.strContentReader.close();
	}
}
